// pages/sita/shareImg/shareImg.js
const app = getApp()
var canvas_width = 0;
var canvas_height = 0
var canvas  = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invitation_Code:'',
    showCanvas:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
 
  gen_invitecode(){
    var that = this
    wx.showLoading({
      title: '生成中',
    })
    wx.request({
      url: app.app_url.BASE_URL+app.app_url.INVITE_CODE,
      method:'POST',
      data:{
        'openid':app.globalData.OpenID
      },
      success(res){
     
        if(res.data.status==200){ 
          that.setData({
            invitation_Code:res.data.inviteCode,
            showCanvas:true
          })

          that.getShareImg()
          
        }else{
          wx.hideLoading()
        }
       
      },
      fail(res){
        wx.hideLoading()
     
      }
    })
  },
  getShareImg(){
    var that = this
    const query = wx.createSelectorQuery()
   
    query.select('#share_canvas')
    .fields({node:true,size:true})
    .exec((res)=>{
     
      canvas = res[0].node
      console.log(canvas)
      const ctx = canvas.getContext('2d')
      const dpr = wx.getSystemInfoSync().pixelRatio
      canvas.width = res[0].width * dpr
      canvas.height = res[0].height * dpr
      canvas_width = canvas.width
      canvas_height = canvas.height
      ctx.fillStyle="white"
      // ctx.fillRect(0,0, canvas_width,canvas_height)
      ctx.arc(40,40,40,Math.PI,Math.PI*1.5)
      ctx.arc(canvas_width-40, 40,40,Math.PI*1.5,0)
      ctx.arc(canvas_width-40, canvas_height-40,40,0,Math.PI*0.5)
      ctx.arc(40, canvas_height-40,40,Math.PI*0.5,Math.PI)
      ctx.closePath()
      ctx.fill()
      // XX55aa
      // ctx.scale(dpr, dpr)
      that.drawQRCodeImg(canvas,ctx,dpr)
    })
    wx.hideLoading()
 
  },
  drawQRCodeImg(canvas,ctx,dpr){
    var that = this
    let img = canvas.createImage()
    img.onload = (e)=>{
      
      var realImgWidth = 70*2*dpr
    
      var realImgHeight = 70*2*dpr
      var imgleft = (canvas.width-realImgWidth)/2
     console.log(imgleft+"-"+5*dpr+"-"+realImgWidth+"-"+realImgHeight)
     that.drawTx(canvas,ctx,dpr,realImgHeight+5*dpr)
      ctx.drawImage(img, imgleft, 5*dpr,realImgWidth,realImgHeight)
      that.drawTx(canvas,ctx,dpr,realImgHeight+5*dpr)
      

     
    }
     img.src = '/images/share_img.jpg'
   
  },
  drawTx(canvas,ctx,dpr,topheight){
    var yqmHeight = 25*dpr
    var yqmNumberHeight = 40*dpr
    ctx.font  = yqmHeight+"px sans-serif"
    var yqm ="邀请码"
    var yqmTx =  ctx.measureText(yqm); 
    var yqmLeft =(canvas.width-yqmTx.width)/2
    ctx.fillStyle = "#000000"
    ctx.fillText(yqm,yqmLeft,  25*dpr +topheight+10*dpr)
  
    var number  = this.data.invitation_Code
    console.log(number)
    ctx.font  = "bold "+yqmNumberHeight+"px sans-serif"
    ctx.fillStyle = "#07C160"
    var numberTx =  ctx.measureText(number); 
    var numberLeft =(canvas.width-numberTx.width)/2
    ctx.fillText(number,numberLeft, yqmHeight+yqmNumberHeight+topheight+20*dpr)
    
  },
  go_share(){
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success () {
              // 用户已经同意保存到相册功能，后续调用 wx.saveImageToPhotosAlbum 接口不会弹窗询问
             that.downLoad()
            },
            fail(res){
              console.log(res)
              wx.showModal({
                title: '提示',
                content: '请点击右上角设置开启添加到相册权限',
                complete: (res) => {
                  wx.openSetting()
                }
              })
            }
          })
        }else{
          that.downLoad()
        }
      }
    })
   
  },
  downLoad(){
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: canvas_width,
      height: canvas_height,
      destWidth: canvas_width,
      destHeight: canvas_height,
      canvas:canvas,
      canvasId: '#share_canvas',
      success(res) {
        console.log(res)
        var made_img = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: made_img,
          success(res){
            wx.showToast({
              title: '邀请码图片下载成功',
            })
          },
          fail(res){
            console.log(res)
          }
        })
    
      },
      fail(res){
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   this.gen_invitecode()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})